package com.kingdee.pda.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kingdee.pda.R;
import com.kingdee.pda.callback.OnCheckBoxChangeListener;
import com.kingdee.pda.callback.OnEditTextChangeListener;
import com.kingdee.pda.entity.QtfInOutEntryInfo;

import java.util.List;

/**
 * @author：dlj
 * @data：2021/9/29 15:10
 * @des：
 */
public class FastAllotTaskListAdapter extends BaseQuickAdapter<QtfInOutEntryInfo, BaseViewHolder> {

    private final OnEditTextChangeListener editTextChangeListener;
    private OnCheckBoxChangeListener listener;
    private Context context;
    private boolean isShowCheckbox = false;

    public boolean isShowCheckbox() {
        return isShowCheckbox;
    }

    public void setShowCheckbox(boolean showCheckbox) {
        isShowCheckbox = showCheckbox;
        notifyDataSetChanged();
    }

    public FastAllotTaskListAdapter(Context context, List<QtfInOutEntryInfo> beans,
                                    OnCheckBoxChangeListener listener, OnEditTextChangeListener editTextChangeListener) {
        super(R.layout.item_fastallot_goodlist, beans);
        this.context = context;
        this.listener = listener;
        this.editTextChangeListener = editTextChangeListener;
    }

    @Override
    protected void convert(BaseViewHolder viewHolder, QtfInOutEntryInfo item) {

        viewHolder.setText(R.id.tv_skucode, item.getItemBarcodeNumber())
                .setText(R.id.tv_outLocationCode, item.getOutlocationNumber())
                .setText(R.id.tv_inLocationCode, item.getInlocationNumber())
                .setText(R.id.tv_skuBarCode, item.getItemskucode())
                .setText(R.id.tv_specName, item.getItemskuname())
                .setText(R.id.tv_invStatusName, item.getInvStatusName())
                .setOnCheckedChangeListener(R.id.checkBox, new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        listener.onCheckChanged(viewHolder.getLayoutPosition(), b);
                    }
                });

        CheckBox checkBox = viewHolder.getView(R.id.checkBox);
        if (isShowCheckbox) {
            checkBox.setVisibility(View.VISIBLE);
        } else {
            checkBox.setVisibility(View.GONE);
        }
        viewHolder.setEnabled(R.id.tv_num, isShowCheckbox);

        EditText et_num = viewHolder.getView(R.id.tv_num);
        if (et_num.getTag() != null && et_num.getTag() instanceof TextWatcher) {
            et_num.removeTextChangedListener((TextWatcher) et_num.getTag());
        }
        et_num.setText(item.getInouttransferqty() + "");
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                try {
                    String num = editable.toString();
                    if (!TextUtils.isEmpty(num)) {
                        editTextChangeListener.onEditTextChanged(viewHolder.getLayoutPosition(), Integer.parseInt(num));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        et_num.addTextChangedListener(textWatcher);

        et_num.setTag(textWatcher);
    }
}
